<?php
/**
 * 
 * OpenID Auth Adapter using Zend Framework
 * 
 * @category Sfer
 * 
 * @package Sfer_Auth
 * 
 * @subpackage Sfer_Auth_OpenID
 * 
 * @author Richard Thomas <richard@phpjack.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 * 
 * @version $Id: OpenID.php 15 2009-12-19 03:47:56Z cyberlot $
 * 
 */
class Sfer_Auth_Adapter_OpenID extends Solar_Auth_Adapter 
{
    protected $_consumer; // We need to keep the Zend consumer class available
    protected $_reg_info; // same for Sref extension
    
    
    /**
     *
     * Handle Openid login, 
     *
     * @return array Info needed to login
     *
     */   
    protected function _processLogin()
    {
        $id = "";
        $result = false;
        if ($this->_consumer->verify($this->_request->get(), $id, $this->_reg_info)) {          
            $props = $this->_reg_info->getProperties();
        }
        if (! is_array($props)) {
            // Authentication failed
            return $result;
        }      
        // complete login
        return array(
            'handle'    => $props['nickname'], // username
            'email'     => $props['email'], // email    
            'moniker'   => $props['nick'], //    
        );
    }
    
    /**
     *
     * Have we gotten a saml Response?
     *
     * @return bool
     *
     */         
    public function isLoginRequest() {
        // Is this an openid request
        if ($this->_request->post('openid_id') || $this->_request->get('openid_mode')) {
            // We are in some sort of openid request, lets get the class
            $storage = new Zend_OpenId_Consumer_Storage();
            $this->_consumer = new Zend_OpenID_Consumer($storage);
            $this->_reg_info = new Zend_OpenID_Extension_Sreg(array(
                            'nickname'  => false,
                            'email'     => false,
                            'fullname'  => false,
                        ));
            // This isn't a login request, its the initial openid build request.. Lets execute login
            if ($this->_request->post('openid_id')) {
                try {
                    if (! $this->_consumer->login($this->_request->post('openid_id'), NULL, NULL, $this->_reg_info)) {
                        echo $this->_consumer->getError();
                    }  
                } catch (Exception $e) {
                    echo $e;
                }
            } else {
                return true;
            }
        }
        return false;
    }

}